package actions;

import java.io.File;

import org.apache.commons.io.FileUtils;

import java.io.IOException; 

import com.opensymphony.xwork2.ActionSupport;

import controllers.AbstractController;




public class UploadFile extends ActionSupport{
		private static final long serialVersionUID = 1L;
	
	
		AbstractController controller = new AbstractController();
	
		private File myFile;
		private String sFileName;
		private int idRender;
		private int idUser;

		private String myFileContentType;
		private String myFileFileName;
		private String destPath;

	 public String execute()
	   {
	      /* 
	       * DO Nothing
	       * And
	       * Everything should work
	       */   
		 return SUCCESS;
	   }
	 public String upload_file(){
		 destPath = ("./depot/Files");
	      
	      System.out.println(".........Upload de fichiers......");
	      System.out.println("Destiantion path: " + destPath);
	      if (sFileName!= null && !sFileName.isEmpty() &&  myFile != null)
	    	 {
		      try{
		     	 System.out.println("Src imported File Real name: " + myFile);
		     	 System.out.println("Src sFileName				: " + sFileName);
		     	 System.out.println("Src id Render				: " + idRender);
		     	 System.out.println("Src id User				: " + idUser);
		     	
		     	 // get file extension from default name
		     	 int ext = myFileFileName.length()- 4;
		     	 sFileName += myFileFileName.substring(ext);
		     	 //putting file in depository
		     	 File destFile  = new File(destPath, (int) Math.floor(Math.random()*101)+sFileName); 
		    	 FileUtils.copyFile(myFile, destFile);
			      // Creating file entry in DB	    	 
		    	 System.out.println("Dst File name: " + destFile.getAbsolutePath());
		    	 boolean result = controller.createFile(sFileName, destFile.getAbsolutePath(), idRender, idUser);
		    	 if (result)
		    		 return SUCCESS;
		    	 else
		    		 return ERROR;
		      }catch(IOException e){
		         e.printStackTrace();
		         return ERROR;
		      } 
		      }
	      else
	    	  return ERROR;
	 }
	   // Getters and setters
	   public String getSFileName() {
		   return sFileName;
	   }
		public void setSFileName(String sFileName) {
			this.sFileName = sFileName;
		}
	   public File getMyFile() {
	      return myFile;
	   }
	   public void setMyFile(File myFile) {
	      this.myFile = myFile;
	   }
	   public String getMyFileContentType() {
	      return myFileContentType;
	   }
	   public void setMyFileContentType(String myFileContentType) {
	      this.myFileContentType = myFileContentType;
	   }
	   public String getMyFileFileName() {
	      return myFileFileName;
	   }
	   public void setMyFileFileName(String myFileFileName) {
	      this.myFileFileName = myFileFileName;
	   }
		public int getIdUser() {
			return idUser;
		}
		public void setIdUser(int idUser) {
			this.idUser = idUser;
		}
	   public int getIdRender() {
		return idRender;
	   }
		public void setIdRender(int idRender) {
			this.idRender = idRender;
		}
}